package com.book.dao;

import com.book.model.User;
import com.book.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    /**
     * 得到用户帐户
     *
     * @param account 账户
     * @return {@link User}
     */
    public User getUserByAccount(String account) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        User user=null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM user WHERE account = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, account);
            rs = stmt.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setId(rs.getLong("id"));
                user.setAccount(rs.getString("account"));
                user.setPassword(rs.getString("password"));;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        return user;
    }
}